package icraf.carbon.gui.model;

import icraf.carbon.core.Zone.DataType;
import icraf.carbon.gui.LCTableModel;
import icraf.carbon.gui.LCTableModel.ValueType;
import icraf.carbon.gui.MatrixPanel;
import icraf.carbon.gui.MenuFactory.MenuProperty;
import icraf.carbon.gui.RowHighlight;
import icraf.carbon.model.ModelProject;
import icraf.common.gui.VerticalLabelUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class ModelIterationSinglePanel extends JPanel {
	private static final long serialVersionUID = 3935329706944049274L;

	
	private ModelProject modelProject;
	protected JTabbedPane tabPanel;


	private boolean isRowHighlight = false;


	private RowHighlight rowHighlight;

	public ModelIterationSinglePanel() {
		super();
		tabPanel = new JTabbedPane(JTabbedPane.LEFT);
		this.setLayout(new BorderLayout());
		this.add(tabPanel);
		JLabel l = new JLabel(MenuProperty.MODEL_PERIOD.getLabel()+ " ");
		l.setFont(new Font("Arial", Font.BOLD, 16));
		l.setOpaque(true);
		l.setBackground(Color.gray);
		l.setForeground(Color.white);
		l.setUI(new VerticalLabelUI());
		l.setHorizontalAlignment(JLabel.RIGHT);
		this.add(l, BorderLayout.WEST);
	}

	public ModelProject getModelProject() {
		return modelProject;
	}

	public void setModelProject(ModelProject modelProject, DataType dataType, ValueType valueType, Object attributes) {
		this.modelProject = modelProject;
		int n = -1;
		if(modelProject != null) {
			n = modelProject.getIterationCount();
		}
		
		while (tabPanel.getTabCount() > n + 1) {
			tabPanel.remove(tabPanel.getTabCount() - 1);
		}
		
		MatrixPanel mPanel;
		for (int i = 0; i < n+1; i++) {
			if (i < tabPanel.getTabCount()) {
				mPanel = (MatrixPanel) tabPanel.getComponentAt(i);
				tabPanel.setTitleAt(i, Integer.toString(i));
			} else {
				mPanel = new MatrixPanel(new LCTableModel(true), true, true, true, true, true);
				tabPanel.addTab(Integer.toString(i), mPanel);
			}
			LCTableModel tm = (LCTableModel)mPanel.getMatrixTableModel();
			tm.setProject(modelProject.getSimulatedProject(i));
			tm.setDataType(dataType, attributes);
			tm.setValueType(valueType);
			if(isRowHighlight) {
				mPanel.setRowHighlight(isRowHighlight, rowHighlight);
			}
			tm.setEditable(false);
		}
		
	}
	

	
	public void setRowHighlight(boolean b, RowHighlight h) {
		isRowHighlight  = b;
		rowHighlight = h;
		MatrixPanel mPanel;
		for (int i = 0; i < tabPanel.getTabCount(); i++) {
			mPanel = (MatrixPanel)  tabPanel.getComponentAt(i);
			mPanel.setRowHighlight(b, h);
		}
		
	}

	public void setCornerSetting(String unitDef, String unitArea,
			boolean isUnitSelectable) {
		MatrixPanel mPanel;
		for (int i = 0; i < tabPanel.getTabCount(); i++) {
			mPanel = (MatrixPanel)  tabPanel.getComponentAt(i);
			mPanel.setUnit(unitDef, unitArea);
			mPanel.setUnitSelectable(isUnitSelectable);
			
		}
		
	}
	
	public void setZoneAreaScale(boolean isZoneAreaScale) {
		MatrixPanel mPanel;
		for (int i = 0; i < tabPanel.getTabCount(); i++) {
			mPanel = (MatrixPanel)  tabPanel.getComponentAt(i);
			mPanel.setZoneAreaScale(isZoneAreaScale);
		}
	}
	
	public void setSelectUnitArea(boolean b) {
		MatrixPanel mPanel;
		for (int i = 0; i < tabPanel.getTabCount(); i++) {
			mPanel = (MatrixPanel)  tabPanel.getComponentAt(i);
			mPanel.setSelectUnitArea(b);
		}
	}

	

}